package cn.wolfcode.p2p.base.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.base.service.ISendVerifyCodeService;
import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import cn.wolfcode.p2p.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.UUID;

@Service
@Transactional
public class SendVerifyCodeServiceImpl implements ISendVerifyCodeService {
        @Autowired
        private LoginInfoMapper loginInfoMapper;
        @Override
        public void sendVerifyCode(String phone) {
                //判断下手机号,判断手机号是否已经注册过
                AssertUtil.instance().isLength(phone.length(), Constants.VALID_PHONE_LENGTH,"手机号码格式不对")
                        .isMobile(phone,"手机号码格式不对")
                        .isFalse(loginInfoMapper.selectCount(phone)>0,"手机号已被注册");
                //判断是否频繁发送
                VerifyCodeVo lastVo = UserContext.getVerifyCodeVo();
                //获取到上一次的时间和这一次的时间对比,10秒内就属于频繁发送
                Date now = new Date();
                if(lastVo!=null){
                        Date lastsendTime = lastVo.getSendTime();
                        //时间工具类判断两个时间的毫秒值的差值,返回两个时间的时间差的秒数,毫秒值除以1000了
                        AssertUtil.instance().isFalse(DateUtil.getSecondsBetween(now,lastsendTime)<Constants.VALID_VERIFYCODE_INTERVAL,"请勿频繁发送验证码");
                }


                //创建一个UUID验证码
                String verifyCode = UUID.randomUUID().toString().substring(0, 4);
                //发送验证码
                System.out.println("您的验证码是" + verifyCode);
                //发送短信
                send(phone,verifyCode);
                //保存到SESSION里
                VerifyCodeVo codeVo = new VerifyCodeVo();
                codeVo.setPhone(phone);
                codeVo.setVerifyCode(verifyCode);
                codeVo.setSendTime(now);
                UserContext.setVerifyCodeVo(codeVo);
        }
        public void send(String phone,String code){
                //Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
                HashMap<String, String> map = new HashMap<>();
                map.put("Uid","陆丰RYU");
                map.put("Key","d41d8cd98f00b204e980");
                map.put("smsMob",phone);
                map.put("smsText","您本次的验证码是:"+code+",请在5分钟内使用");

                try {
                        String result = HttpUtil.sendHttpRequest("http://utf8.api.smschinese.cn/", map);
                       /* if("-3".equals(result)){
                                throw  new CustomException("短信发送失败[-3],请联系客服!");
                        }*/
                }catch(CustomException e){
                        throw  new CustomException(e.getMessage());
                } catch (Exception e) {
                        throw  new CustomException("系统异常,请稍后再试!");
                }
        }

}
